<?php
class NavModel extends Model {
    public function __construct() {
        parent::__construct();
        $this->fields = array('id','name','info','sort','sid','price','brand');
        $this->tables = array(DB_FREFIX.'nav');
        $this->check = new NavCheck();
        //转义post和get获得的各种参数
        list(
                $this->R['sid'],
                $this->R['name'],
                $this->R['id'],
                $this->R['navid'],
                $this->R['attr']
        ) = $this->getRequest()->getParams(
            array(
                isset($_GET['sid']) ? $_GET['sid'] : null,
                isset($_POST['name']) ? $_POST['name'] : null,
                isset($_GET['id']) ? $_GET['id'] : null,
                isset($_GET['navid']) ? $_GET['navid'] : null,
                isset($_GET['attr']) ? $_GET['attr'] : null
            )
        );
    }

    //查找所有nav，用于商品添加时
    public function findAllNav() {
        $allNav = parent::select($this->tables,array('id','name','sid'),array('order'=>'sort ASC'));
        foreach($allNav as $key=>$value) {
            $value->sid == 0 ? $mainNav[] = $value : $childNav[] = $value;
        }
        foreach($mainNav as $key=>$value) {
            foreach($childNav as $k=>$v) {
                if($value->id == $v->sid) {
                    //为了使用smarty的模板的内置函数，需要对对象进行特定的规范化
                    $value->child[$v->id] = $v->name;
                }
            }
        }
        return $mainNav;
    }

    //查找前台十条nav
    public function findFrontTenNav() {
        return parent::select($this->tables,array('id','name'),array('where'=>array('sid=0'),'limit'=>10,'order'=>'sort ASC'));
    }

    public function findListPrice() {
        $onePrice = parent::select($this->tables,array('price'),array('where'=>array('id='.$this->R['navid'])));
        $listPrice = unserialize(htmlspecialchars_decode($onePrice[0]->price));
        if(!Validate::isNullString($listPrice)) {
            foreach($listPrice as $key=>$value) {
                unset($listPrice[$key]);
                $newK = str_replace(' - ',',',$value);
                $listPrice[$newK] = $value;
            }
        }
        return $listPrice;
    }

    public function showNav() {
        $mainNav = $childNav = $resultNav = array();
        //获取所有nav
        $allNav = parent::select($this->tables,array('id','name','sid'));
        $resultNav = Tree::getInstance()->getTree($allNav,$_GET['navid']);
        if(isset($resultNav[0]->child)) {
            //获取每个子导航类别包含的商品数量
            foreach($resultNav[0]->child as $key=>$value) {
                $count = parent::select(array(DB_FREFIX.'goods'),array('COUNT(*) AS count'),array('where'=>array("nav='{$value->id}' AND is_up=1")));
                $value->count = $count[0]->count;
            }
        }
        return $resultNav;
    }

    public function findAll() {
        $condition = array('sid='.$this->R['sid']);
        if(!isset($_GET['sid'])) {
            return parent::select($this->tables,$this->fields,array('where'=>$condition,'limit'=>$this->limit,'order'=>'sort ASC'));
        } else {
            $allNav = parent::select($this->tables,array('id','name','info','sort','sid','brand'),array('where'=>$condition,'limit'=>$this->limit,'order'=>'sort ASC'));
            $allBrand = Tool::setObjectArray(parent::select(array(DB_FREFIX.'brand'),array('id','name')),'id','name');
            if(isset($allNav)) {
                foreach($allNav as $key=>$value) {
                    if($value->brand == '') {
                        $value->brand = '其他品牌';
                    } else {
                        $brandArr = unserialize(htmlspecialchars_decode($value->brand));
                        $value->brand = '';
                        if(isset($brandArr)) {
                            foreach($brandArr as $k=>$v) {
                                $value->brand .= $allBrand[$v].'|';
                            }
                        }
                        $value->brand = substr($value->brand,0,-1);
                    }
                }
            }
        }
        return $allNav;
    }
    public function findGoodsBrand() {
        $oneNav = parent::select($this->tables,array('brand'),array('where'=>array('id='.$this->R['id'])));
        return unserialize(htmlspecialchars_decode($oneNav[0]->brand));
    }
    public function findGoodsPrice() {
        $oneNav = parent::select($this->tables,array('price'),array('where'=>array('id='.$this->R['id'])));
        return unserialize(htmlspecialchars_decode($oneNav[0]->price));
    }
    public function findFrontBrand() {
        $oneNav = parent::select($this->tables,array('brand'),array('where'=>array('id in ('.$this->getNavId().')')));
        $brandId = '';
        foreach($oneNav as $key=>$value) {
            if(empty($value->brand)) {
                unset($value->brand);
                continue;
            }
            $brandId .= implode(',',unserialize(htmlspecialchars_decode($value->brand))).',';
        }
        $brandId = substr($brandId,0,-1);
        if(empty($brandId)) return array('other'=>'其他品牌');
        $brand = Tool::setObjectArray(parent::select(array(DB_FREFIX.'brand'),array('id','name'),array('where'=>array('id in('.$brandId.')'))),'id','name');
        return $brand;
    }

    public function findOne() {
        if(isset($_GET['sid'])) {
            $condition = array('id='.$this->R['sid']);
            return parent::select($this->tables,array('name'),array('where'=>$condition));
        }
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        return parent::select($this->tables,array('id','name','info','sid','brand'),array('where'=>$condition,'limit'=>'1'));
    }
    public function findFatherNav() {
        $condition = array('id='.$this->R['sid']);
        return parent::select($this->tables,array('name'),array('where'=>$condition));
    }
    public function add() {
        $condition = array('name='.$this->R['name']);
        //检验用户名是否重复
        if(!$this->check->addCheck($this, $condition)) $this->check->displayError();
        $needData = $this->getRequest()->filter($this->fields);
        $needData['sort'] = $this->nextId($this->tables[0]);
        $needData['sid'] = isset($_POST['sid']) ? $_POST['sid'] : 0;
        if(isset($needData['brand'])) $needData['brand'] = serialize($_POST['brand']);
        if(isset($needData['price']))$needData['price'] = serialize($_POST['price']);
        return parent::add($needData);
    }
    public function update() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->updateCheck($this)) $this->check->displayError();
        $updateData = $this->getRequest()->filter(array('id','name','info','brand','price'));
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        $updateData['brand'] = isset($updateData['brand']) ? serialize($updateData['brand']) : '';
        $updateData['price'] = isset($updateData['price']) ? serialize($updateData['price']) : '';
        return parent::update($updateData,$condition);
    }
    //删除
    public function delete() {
        $condition = array('id='.$this->R['id'],'rule'=>'or','sid='.$this->R['id']);
        if(!$this->check->oneCheck($this,array('id='.$this->R['id']))) $this->check->displayError();
        return parent::delete($condition);
    }
    public function sort() {
        foreach($_POST['sort'] as $id=>$sort) {
            if(!Validate::isNumber($sort)) continue;
            $condition = array('id='.$id);
            parent::update(array('sort'=>$sort),$condition);
        }
        return true;
    }
    public function checkNavName() {
        $condition = array('name='.$this->R['name']);
        $this->check->checkNavName($this,$condition);
    }
    public function total() {
        $condition = array('sid='.$this->R['sid']);
        return parent::total(array('where'=>$condition));
    }
    private function getNavId() {
        $idArr = parent::select(array(DB_FREFIX.'nav'),array('id'),array('where'=>array('sid='.$this->R['navid'])));
        $id = '';
        if(isset($idArr)) {
            foreach($idArr as $value) {
                $id .= $value->id.',';
            }
            $id = substr($id,0,-1);
        } else {
            $id = $_GET['navid'];
        }
        return $id;
    }


}

?>